package com.salton123.groupbargain.ui.fm

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.hjq.bar.TitleBar
import com.salton123.arch.mvvm.BaseMvvmRefreshFragment
import com.salton123.bmob.bargain.BargainItem
import com.salton123.groupbargain.R
import com.salton123.groupbargain.mvvm.ViewModelFactory
import com.salton123.groupbargain.mvvm.viewmodel.BargainViewModel
import com.salton123.groupbargain.ui.adapter.BargainRecommendItemAdapter
import kotlinx.android.synthetic.main.common_layout_refresh_loadmore.*

/**
 * User: newSalton@outlook.com
 * Date: 2020-01-25 16:55
 * ModifyTime: 16:55
 * Description:
 */
class BarginRecommendItemComp : BaseMvvmRefreshFragment<BargainViewModel, BargainItem>(),
    OnItemClickListener {


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
    }

    private val mAdapter by lazy { BargainRecommendItemAdapter() }

    override fun onBindWrapRefresh(): WrapRefresh = WrapRefresh(refreshLayout, mAdapter)

    override fun onBindViewModelFactory(): ViewModelProvider.Factory = ViewModelFactory

    override fun onBindViewModel(): Class<BargainViewModel> = BargainViewModel::class.java

    override fun initViewObservable() {
        mViewModel.mBargainRecommendEvent
            .observe(this, Observer<List<BargainItem>> {
                showSuccessView()
                mAdapter.setNewData(it as MutableList<BargainItem>?)
            })
    }

    override fun initVariable(savedInstanceState: Bundle?) {
    }

    override fun initViewAndData() {
        recyclerView.layoutManager = LinearLayoutManager(activity())
        recyclerView.setHasFixedSize(true)
        mAdapter.onAttachedToRecyclerView(recyclerView)
    }

    override fun initListener() {
        super.initListener()
        mAdapter.setOnItemClickListener(this)
        mViewModel.init()
    }

    override fun fetchTitleBar(): TitleBar? {
        return null
    }

    override fun getLayout(): Int = R.layout.common_layout_refresh_loadmore

}